package com.sqy.read;

import com.sqy.common.Num;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;

import java.io.*;
import java.util.regex.Pattern;

public class ReadWord {

    public static void read(String path,String encoding) throws Exception {

        //读取word内容
        String buffer = "";
        if (path.endsWith(".doc")) {
            InputStream is = new FileInputStream(new File(path));
            WordExtractor ex = new WordExtractor(is);
            buffer = ex.getText();
        } else if (path.endsWith(".docx")) {
            OPCPackage opcPackage = POIXMLDocument.openPackage(path);
            POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
            buffer = extractor.getText();

            opcPackage.close();
        }

        //写入txt文件
        FileWriter fw = new FileWriter(Num.PATH);
        fw.write(buffer);
        fw.close();

        //调用读取txt
        ReadTxt.read(Num.PATH,encoding);
    }
}
